﻿using BigEgg.Framework.UnitTesting;
using FMStudio.Documents.TemplateDocument.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;

namespace FMStudio.Documents.TemplateDocument.Test
{
    [TestClass]
    public class TemplateDocumentTest
    {
        private string TestPath1 = Path.Combine(Environment.CurrentDirectory, "Template1.fmstemplate");
        private string TestPath2 = Path.Combine(Environment.CurrentDirectory, "Template2.fmstemplate");

        [TestInitialize]
        public void TestCleanup()
        {
            if (File.Exists(TestPath1))
                File.Delete(TestPath1);
            if (File.Exists(TestPath2))
                File.Delete(TestPath2);
        }


        [TestMethod]
        public void DocumentTest()
        {
            TemplateDocument itemDocument = new TemplateDocument(TemplateType.Item);
            Assert.AreEqual("1.0.0", itemDocument.FormatVersion);
            Assert.AreEqual(TemplateType.Item, itemDocument.Type);
            Assert.IsNotNull(itemDocument.TemplateData);
            Assert.IsNull(itemDocument.WizardExtension);
            Assert.IsTrue(itemDocument.TemplateContent is ItemTemplateContent);

            TemplateDocument projectDocument = new TemplateDocument(TemplateType.Project);
            Assert.AreEqual("1.0.0", projectDocument.FormatVersion);
            Assert.AreEqual(TemplateType.Project, projectDocument.Type);
            Assert.IsNotNull(projectDocument.TemplateData);
            Assert.IsNull(projectDocument.WizardExtension);
            Assert.IsTrue(projectDocument.TemplateContent is ProjectTemplateContent);
        }

        [TestMethod]
        public void DocumentModifyTest()
        {
            TemplateDocumentType documentType = new TemplateDocumentType();
            TemplateDocument document = documentType.New(TestPath1) as TemplateDocument;
            documentType.Save(document);

            Assert.IsFalse(document.IsModified);
            AssertHelper.PropertyChangedEvent(document, x => x.IsModified, () => document.TemplateData.Name = "NewName");
            Assert.IsTrue(document.IsModified);
            documentType.Save(document);

            Assert.IsFalse(document.IsModified);
            document.WizardExtension = new TemplateWizard();
            AssertHelper.PropertyChangedEvent(document, x => x.IsModified, () => document.WizardExtension.Assembly = "SomeAssembly");
            Assert.IsTrue(document.IsModified);
            documentType.Save(document);

            Assert.IsFalse(document.IsModified);
            AssertHelper.PropertyChangedEvent(document, x => x.IsModified, () => (document.TemplateContent as ProjectTemplateContent).TargetName = "NewName.fmproj");
            Assert.IsTrue(document.IsModified);
            documentType.Save(document);

            Assert.IsFalse(document.IsModified);
            AssertHelper.PropertyChangedEvent(document, x => x.IsModified,
                () => document.TemplateContent.Items.Add(new Folder() { Name = "Folder", TargetFolderName = "NewFolder" }));
            Assert.IsTrue(document.IsModified);
        }

        [TestMethod]
        public void PropertiesWithNotification()
        {
            TemplateDocumentType documentType = new TemplateDocumentType();
            TemplateDocument document = documentType.New(TestPath1) as TemplateDocument;

            AssertHelper.PropertyChangedEvent(document, x => x.WizardExtension, () => document.WizardExtension = new TemplateWizard());
            Assert.IsNotNull(document.WizardExtension);

            AssertHelper.PropertyChangedEvent(document, x => x.TemplateData, () => document.TemplateData.Description = "New Description");
            Assert.AreEqual("New Description", document.TemplateData.Description);

            AssertHelper.PropertyChangedEvent(document, x => x.WizardExtension, () => document.WizardExtension.Assembly = "SomeAssembly");
            Assert.AreEqual("SomeAssembly", document.WizardExtension.Assembly);
        }
    }
}
